
var apiHost = wx.getStorageSync('apiHost');
var yunId = wx.getStorageSync('yunId');

var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this ; 
    that.getWelcomeImage(); //获取欢迎页面的图片
    
    that.getSystemHeight();
  },

  /**
   * 获取屏幕高度
   */
  getSystemHeight : function(){
    var that = this;
    //获取屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          viewHeght: res.windowHeight
        });
      }
    });
  },

  //获取欢迎页面的图片
  getWelcomeImage: function () {
    var that = this;

    //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "xcx/login/getWelcomeImage",
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg

        if (code == "10000") {
          var welcomeImage = res.data.data;

          that.setData({
            welcomeImage: welcomeImage
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading
      },
      fail : function(){
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: "网络异常",
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

  //按钮获取用户信息
  loginHd: function (res) {
    var that = this;

    var errMsgFlg = res.detail.errMsg

    if ("getUserInfo:ok" == errMsgFlg) {
      var userCon = res.detail
      wx.login({
        success: function (r) {
          var code = r.code;//登录凭证
          if (code) {
            that.VERBurl(userCon, code)
          } else {
            wx.showToast({
              title: '获取用户登录态失败！' + r.errMsg,
              icon: 'none',
              duration: 2000,
              mask: true
            });
          }
        },
        fail: function () {
          wx.hideLoading();   //隐藏加载loading
          wx.showToast({
            title: '登陆失败',
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
      })
    }
  },

  /**
  * 解密用户登录完成后的url
  */
  VERBurl: function (res, code) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });
    //3.请求自己的服务器，解密用户信息 获取unionId等加密信息  (注意：这个东西必须需要 开放平台 授权)
    wx.request({
      url: apiHost + 'xcx/UserInfo/decodeUserInfo',//自己的服务接口地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        encryptedData: res.encryptedData,
        iv: res.iv,
        code: code,
        Appid: Appid,
        Secret: Secret
      },
      success: function (data) {
        //4.解密成功后 获取自己服务器返回的结果
        if (data.data.status == 1) {
          var userInfo_ = data.data.userInfo;

          console.log(userInfo_)

          var openIdFl = userInfo_.openId;
          var phone = userInfo_.phone;

          wx.setStorageSync('userId', userInfo_.unionId);
          wx.setStorageSync('openId', openIdFl);
          wx.setStorageSync('phone', phone);
          wx.setStorageSync('session_token', userInfo_.session_key);
          wx.setStorageSync('userInfo', userInfo_);

          that.platformLogin(openIdFl)  //平台登录
        } else {
          wx.hideLoading();   //隐藏加载loading
          wx.showToast({
            title: '网络异常',
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        
        wx.hideLoading();   //隐藏加载loading
      },
      fail: function () {
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: true
        });
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 平台登录
   */
  platformLogin: function (openIdFl) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + 'xcx/login/in',//自己的服务接口地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openId: openIdFl,
        yunId: yunId
      },
      success: function (data) {
        var code = data.data.code;     //调用是否成功标识
        var dataFlag = data.data.data; //判断是云店主的标识
        wx.hideLoading();   //隐藏加载loading

        //如果调用成功 判断跳转
        if (code == "10000") {
          // 跳转云店主主页
          if (dataFlag == "storeBoss") {
            wx.navigateTo({
              url: '../yunShopIndex/yunShopIndex',
              success: function () {
                wx.hideLoading();   //隐藏加载loading
              },
            });
          } else
            // 跳转普通用户主页
            if (dataFlag == "userCommon") {
              wx.switchTab({
                url: '../Zindex/Zindex',
                success: function () {
                  wx.hideLoading();   //隐藏加载loading
                },
              });
            }
        }
      },
      fail: function () {
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: true
        });

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})